<?php

    namespace App\Http\Controllers\Admin;

    use App\Models\Admin\User;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Session;
    use Validator;
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;

    class LoginController extends Controller
    {
        //
        public function loginGet(Request $request)
        {
            return view('/admin/login/login');
        }

        public function loginPost(Request $request)
        {
//       print_r($request->input());
            $params    = $request->input();
            $validator = Validator::make($request->all(), [
                'email'    => 'required|email',
                'password' => 'required|',
            ]);

            if ($validator->fails()) {
                return redirect('/admin/login')
                    ->withErrors($validator)
                    ->withInput()
                    ;
            }
            $where               = [];
            $where[ 'password' ] = md5($params[ 'password' ]);
            $where[ 'email' ]    = $params[ 'email' ];
            $userInfo            = User::where($where)->first()->toArray();
            if ( !empty($userInfo)) {
                Session::put('admin',$userInfo);
                return redirect('admin/index');
            }
            else {
                return redirect('/admin/login')
                    ->withInput();
            }
        }
    }
